//
//  ViewController.m
//  11-26date
//
//  Created by mac on 15-11-26.
//  Copyright (c) 2015年 huangfeng. All rights reserved.
//

#import "ViewController.h"
#import "inputViewController.h"
@interface ViewController ()<inputFinishDelegate,UITableViewDataSource>
{
    NSArray *date;
    NSArray *tels;
    NSIndexPath *curIndexPath;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.title = @"通讯录";
//    date = @[
//             @"傻逼",
//             @"二货",
//             @"蠢蛋"
//             ];
//    tels = @[
//            @"15348248925",
//            @"14862785625",
//            @"18627856334"
//            ];
    
    NSUserDefaults *df =[NSUserDefaults standardUserDefaults];
//    [df setObject:date forKey:@"date"];
//    [df setObject:tels forKey:@"tels"];
//    [df synchronize];
    
    date = [df objectForKey:@"date"];
    tels = [df objectForKey:@"tels"];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return date.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.textLabel.text = date[indexPath.row];
    cell.detailTextLabel.text = tels[indexPath.row];
    return cell;
}


- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSMutableArray * arr =date.mutableCopy;
        [arr removeObjectAtIndex:indexPath.row];
        date = arr;
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    inputViewController *con = [[inputViewController alloc]initWithStyle:UITableViewStyleGrouped];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    curIndexPath = indexPath;
    con.tel = cell.detailTextLabel.text;
    con.delegate = self;
    [self.navigationController pushViewController:con animated:YES];
    
    UIApplication *app = [UIApplication sharedApplication];
    NSString *tel = [NSString stringWithFormat:@"sms://%@",tels[indexPath.row]];
    [app openURL:[NSURL URLWithString:tel]];
}

- (void)inputFinished:(NSString *)text{
    NSLog(@"indexPath:%@",[self.tableView indexPathForSelectedRow]);
    NSMutableArray *arr = tels.mutableCopy;
    [arr replaceObjectAtIndex:curIndexPath.row withObject:text];
    tels = arr;
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
